/**
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

//SMUIML v0.4

package diuf.diva.hephaistk.xml.smuiml;

import it.unina.lab.costants.Constants;

import java.util.Vector;

import diuf.diva.hephaistk.xml.Attribute;
import diuf.diva.hephaistk.xml.HephaisTKParsingException;
import diuf.diva.hephaistk.xml.XmlGeneric;

public class Transition extends Smuiml_Generic {
	/**
	 *
	 */
	private static final long serialVersionUID = 1690190645798898647L;
	public final static String NAME = "transition";

	private Vector<Trigger> triggers = null;
	private Vector<Result> results = null;
	private Vector<FusionElement> fusionElements = null;

	private Attribute attName = null;
	private Attribute attClause = null;
	private Attribute attLeadTime = null;

	public Transition(){
		super();
		setName(NAME);
		triggers = new Vector<Trigger>();
		results = new Vector<Result>();
		fusionElements = new Vector<FusionElement>();
	}

	public Vector<Trigger> getTriggers(){
		return triggers;
	}

	public Trigger getTrigger(int index){
		return triggers.get(index);
	}

	public int triggersSize(){
		return triggers.size();
	}

	public void addTrigger(Trigger trigger){
		triggers.add(trigger);
	}

	public void setTriggers(Vector<Trigger> triggers){
		this.triggers = triggers;
	}

	public Vector<Result> getResults(){
		return results;
	}

	public Result getResult(int index){
		return results.get(index);
	}

	public int resultsSize(){
		return results.size();
	}

	public void addResult(Result result){
		results.add(result);
	}

	public void setResults(Vector<Result> results){
		this.results = results;
	}

	public Vector<FusionElement> getFusionElements(){
		return fusionElements;
	}

	public FusionElement getFusionElement(int index){
		return fusionElements.get(index);
	}

	public int fusionElementsSize(){
		return fusionElements.size();
	}

	public void addFusionElement(FusionElement fe){
		fusionElements.add(fe);
	}

	public void setFusionElements(Vector<FusionElement> fusionElements){
		this.fusionElements = fusionElements;
	}

	public Attribute getAttLeadTime() {
		return attLeadTime;
	}

	public void setAttLeadTime(String attLeadTime) {
		this.attLeadTime = new Attribute("leadtime", attLeadTime);
	}

	/**
	 * @return the attName
	 */
	public Attribute getAttName() {
		return attName;
	}

	/**
	 * @param attName the attName to set
	 */
	public void setAttClause(String attClause) {
		this.attClause = new Attribute("clause", attClause);
	}

	/**
	 * @return the attName
	 */
	public Attribute getAttClause() {
		return attClause;
	}

	/**
	 * @param attName the attName to set
	 */
	public void setAttName(String attName) {
		this.attName = new Attribute("name", attName);
	}

	public void addElement(XmlGeneric element) throws HephaisTKParsingException {
		if(element instanceof Result){
			addResult((Result)element);
		}else if(element instanceof Trigger){
			addTrigger((Trigger)element);
		}else if(element instanceof FusionElement){
			addFusionElement((FusionElement)element);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected element: "+element.getName()+"; only <"+Trigger.NAME+"> or <"+Result.NAME+"> or par_and, par_or, seq_and, seq_or allowed");
		}
	}

	@Override
	public void addAttribute(Attribute attribute) throws HephaisTKParsingException{
		super.addAttribute(attribute);
		String attName = attribute.getName();
		if (attName.indexOf(':') != -1) {
			attName = attName.substring(attName.indexOf(':')+1);
		}
		addAttribute(attName, attribute.getValue());
	}

	public void addAttribute(String name, String value) throws HephaisTKParsingException{
		if(name.equalsIgnoreCase("name")){
			setAttName(value);
		}else if (name.equalsIgnoreCase("clause")){
			setAttClause(value);
		}else if(name.equalsIgnoreCase("leadtime")){
				setAttLeadTime(value);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected attribute: "+name);
		}
	}

	public String toString(int indent) {
		StringBuilder ret = new StringBuilder();
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("<"+NAME+(attName != null? " name=\""+attName.getValue()+"\"" : "")+
				(attClause != null? " clause=\""+attClause.getValue()+"\"" : "")+
				(attLeadTime != null? " leadtime=\""+attLeadTime.getValue()+"\"" : "")+">\n");
		for (int i = 0; i < triggers.size(); i++){
			ret.append(triggers.get(i).toString(indent + Constants.INDENT_PREFIX));
		}
		for (int i = 0; i < results.size(); i++){
			ret.append(results.get(i).toString(indent + Constants.INDENT_PREFIX));
		}
		for (int i = 0; i < fusionElements.size(); i++){
			ret.append(fusionElements.get(i).toString(indent + Constants.INDENT_PREFIX));
		}
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("</"+NAME+">\n");
		return ret.toString();
	}
}
